import { Navigate, RouteObject } from "react-router-dom";
import { lazy, Suspense } from "react";
import { Spin } from "./Spin";
import AppLayout from "@/layout";
import AuthRoute from "./AuthRoute";

const Management = lazy(() => import("@/pages/management"));
const Record = lazy(() => import("@/pages/record"));
const Assets = lazy(() => import("@/pages/assets"));

const Login = lazy(() => import("@/pages/login"));

const NotFound = lazy(() => import("@/pages/404"));

const NotPermission = lazy(() => import("@/pages/403"));

const Upload = lazy(() => import("@/pages/upload"));

const DataQuery = lazy(() => import("@/pages/data/query"));

const LogDataQuery= lazy(()=>import("@/pages/log/query"))

// 路由懒加载的封装
const LazyLoad = (module: ReturnType<typeof lazy>) => {
  const Com = module;
  return (
    <Suspense fallback={<Spin size="large" />}>
      <Com />
    </Suspense>
  );
};

const routes: RouteObject[] = [
  {
    path: "/",
    element: (
      // <AuthRoute>
      <AppLayout />
      // </AuthRoute>
    ),
    children: [
      {
        index: true,
        element: <Navigate to="/login" />,
      },
      {
        path: "management",
        element: LazyLoad(Management),
      },
      {
        path: "record",
        element: LazyLoad(Record),
      },
      {
        path: "upload",
        element: LazyLoad(Upload),
      },
      {
        path: "assets",
        children: [
          {
            index: true,
            element: <Navigate to="/assets/report_register_info" />,
          },
          {
            path: ":taskKey",
            element: LazyLoad(Assets),
          },
        ],
      },
      {
        path: "data",
        children: [
          {
            index: true,
            element: <Navigate to="/data/query" />,
          },
          {
            path: "query",
            element: LazyLoad(DataQuery),
          },
          {
            path: "log",
            element: LazyLoad(LogDataQuery),
          },
        ],
      },
    ],
  },
  {
    path: "/login",
    element: LazyLoad(Login),
  },
  {
    path: "/403",
    element: LazyLoad(NotPermission),
  },
  {
    path: "/404",
    element: LazyLoad(NotFound),
  },
  {
    path: "*",
    element: <Navigate to="/404" />,
  },
];

export default routes;
